home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / grafica / amhelios / transfm3.h < prev    next >
C/C++ Source or Header  |  1999-01-01  |  5KB  |  186 lines

  1. ////////////////////////////////////////////////////////////
  2. //
  3. //  TRANSFM3.H - 3-D Linear Transformation Class
  4. //
  5. //  Version:    1.03A
  6. //
  7. //  History:    94/08/23 - Version 1.00A release.
  8. //              94/12/16 - Version 1.01A release.
  9. //              95/02/05 - Version 1.02A release.
  10. //              95/07/21 - Version 1.02B release.
  11. //              96/02/14 - Version 1.02C release.
  12. //              96/04/01 - Version 1.03A release.
  13. //
  14. //  Compilers:  Microsoft Visual C/C++ Professional V1.5
  15. //              Borland C++ Version 4.5
  16. //
  17. //  Author:     Ian Ashdown, P.Eng.
  18. //              byHeart Software Limited
  19. //              620 Ballantree Road
  20. //              West Vancouver, B.C.
  21. //              Canada V7S 1W3
  22. //              Tel. (604) 922-6148
  23. //              Fax. (604) 987-7621
  24. //
  25. //  Copyright 1994-1996 byHeart Software Limited
  26. //
  27. //  The following source code has been derived from:
  28. //
  29. //    Ashdown, I. 1994. Radiosity: A Programmer's
  30. //    Perspective. New York, NY: John Wiley & Sons.
  31. //
  32. //  It may be freely copied, redistributed, and/or modified
  33. //  for personal use ONLY, as long as the copyright notice
  34. //  is included with all source code files.
  35. //
  36. ////////////////////////////////////////////////////////////
  37.  
  38. #ifndef _3D_TRANS_H
  39. #define _3D_TRANS_H
  40.  
  41. #include "vector3.h"
  42.  
  43. class Transform3        // 3-D linear transformation
  44. {
  45.   private:
  46.     double scale_x;     // x-axis scaling factor
  47.     double scale_y;     // y-axis scaling factor
  48.     double scale_z;     // z-axis scaling factor
  49.     double trans_x;     // x-axis translation distance
  50.     double trans_y;     // y-axis translation distance
  51.     double trans_z;     // z-axis translation distance
  52.     double rot_x;       // x-axis rotation (in radians)
  53.     double rot_y;       // y-axis rotation (in radians)
  54.     double rot_z;       // z-axis rotation (in radians)
  55.     double m[3][4];     // Transformation matrix
  56.  
  57.     void Identity()     // Initialize identity matrix
  58.     {
  59.       int i, j;         // Loop indices
  60.  
  61.       for (i = 0; i < 3; i++)
  62.         for (j = 0; j < 4; j++)
  63.         {
  64.           if (i == j)
  65.             m[i][j] = 1.0;
  66.           else
  67.             m[i][j] = 0.0;
  68.         }
  69.     }
  70.  
  71.     // Note: s_val is sine of rotation angle
  72.     //       c_val is cosine of rotation angle
  73.  
  74.     // Rotate counterclockwise about x-axis
  75.     void RotateX( double s_val, double c_val )
  76.     {
  77.       int i;        // Loop index
  78.       double temp;  // Temporary variable
  79.  
  80.       for (i = 0; i < 4; i++)
  81.       {
  82.         temp = m[1][i] * c_val - m[2][i] * s_val;
  83.         m[2][i] = m[1][i] * s_val + m[2][i] * c_val;
  84.         m[1][i] = temp;
  85.       }
  86.     }
  87.  
  88.     // Rotate counterclockwise about y-axis
  89.     void RotateY( double s_val, double c_val )
  90.     {
  91.       int i;        // Loop index
  92.       double temp;  // Temporary variable
  93.  
  94.       for (i = 0; i < 4; i++)
  95.       {
  96.         temp = m[0][i] * c_val + m[2][i] * s_val;
  97.         m[2][i] = -m[0][i] * s_val + m[2][i] * c_val;
  98.         m[0][i] = temp;
  99.       }
  100.     }
  101.  
  102.     // Rotate counterclockwise about z-axis
  103.     void RotateZ( double s_val, double c_val )
  104.     {
  105.       int i;        // Loop index
  106.       double temp;  // Temporary variable
  107.  
  108.       for (i = 0; i < 4; i++)
  109.       {
  110.         temp = m[0][i] * c_val - m[1][i] * s_val;
  111.         m[1][i] = m[0][i] * s_val + m[1][i] * c_val;
  112.         m[0][i] = temp;
  113.       }
  114.     }
  115.  
  116.     void Scale()
  117.     {
  118.       m[0][0] *= scale_x;
  119.       m[1][1] *= scale_y;
  120.       m[2][2] *= scale_z;
  121.     }
  122.  
  123.     void Translate()
  124.     {
  125.       m[0][3] += trans_x;
  126.       m[1][3] += trans_y;
  127.       m[2][3] += trans_z;
  128.     }
  129.  
  130.    public:
  131.     Transform3()
  132.     {
  133.       scale_x = scale_y = scale_z = 1.0;
  134.       trans_x = trans_y = trans_z = 0.0;
  135.       rot_x = rot_y = rot_z = 0.0;
  136.  
  137.       Identity();
  138.     }
  139.  
  140.     // Set scaling factors
  141.     void SetScale( double sx, double sy, double sz )
  142.     { scale_x = sx; scale_y = sy; scale_z = sz; }
  143.  
  144.     // Set translation distances
  145.     void SetTranslation( double tx, double ty, double tz )
  146.     { trans_x = tx; trans_y = ty; trans_z = tz; }
  147.  
  148.     // Set rotation angles
  149.     void SetRotation( double rx, double ry, double rz )
  150.     { rot_x = rx; rot_y = ry; rot_z = rz; }
  151.  
  152.     void BuildTransform()
  153.     {
  154.       Identity();       // Initialize identity matrix
  155.  
  156.       Scale();          // Concatenate scale matrix
  157.  
  158.       // Concatenate rotation matrices
  159.       RotateX(sin(rot_x), cos(rot_x));
  160.       RotateY(sin(rot_y), cos(rot_y));
  161.       RotateZ(sin(rot_z), cos(rot_z));
  162.  
  163.       Translate();      // Concatenate translation matrix
  164.     }
  165.  
  166.     // Premultiply point by 3-D transformation matrix
  167.     void Transform( Point3 *pp )
  168.     {
  169.       Point3 temp;      // Temporary 3-D point
  170.  
  171.       temp.SetX(m[0][0] * pp->GetX() + m[0][1] * pp->GetY()
  172.           + m[0][2] * pp->GetZ() + m[0][3]);
  173.       temp.SetY(m[1][0] * pp->GetX() + m[1][1] * pp->GetY()
  174.           + m[1][2] * pp->GetZ() + m[1][3]);
  175.       temp.SetZ(m[2][0] * pp->GetX() + m[2][1] * pp->GetY()
  176.           + m[2][2] * pp->GetZ() + m[2][3]);
  177.  
  178.       pp->SetX(temp.GetX());
  179.       pp->SetY(temp.GetY());
  180.       pp->SetZ(temp.GetZ());
  181.     }
  182. };
  183.  
  184. #endif
  185.  
  186.